package com.itheima.appserver.controller;

import com.itheima.appserver.config.MyCache;
import com.itheima.appserver.service.CommentsService;
import com.itheima.appserver.service.PublishService;
import com.itheima.commons.vo.reception.PageResult;
import com.itheima.commons.pojo.domain.QuanZiVo;
import com.itheima.commons.vo.reception.VisitorsVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;

/**
 * @author yaohu
 * @date 2021/12/7
 * @Version 1.0
 * @describe 圈子相关请求
 */
@RestController
@RequestMapping("/movements")
public class PublishController {

    @Autowired
    private PublishService publishService;

    @Autowired
    private CommentsService commentsService;

    /**
     * 发布动态
     */
    @PostMapping
    public ResponseEntity movements(@RequestParam("textContent") String textContent,
                                    @RequestParam(value = "location", required = false) String location,
                                    @RequestParam(value = "latitude", required = false) String latitude,
                                    @RequestParam(value = "longitude", required = false) String longitude,
                                    @RequestParam(value = "imageContent", required = false) MultipartFile[] multipartFile) throws IOException {
        publishService.publishMovement(textContent, location, latitude, longitude, multipartFile);
        return ResponseEntity.ok(null);
    }

    /**
     * 查询好友动态
     */
    @GetMapping
    @MyCache(cacheTime = 45)
    public ResponseEntity movements(@RequestParam(defaultValue = "1") Integer page,
                                    @RequestParam(defaultValue = "10") Integer pagesize) {
        PageResult pr = publishService.findFriendMovements(page, pagesize);
        return ResponseEntity.ok(pr);
    }

    /**
     * 查询推荐动态
     */
    @GetMapping("/recommend")
    @MyCache
    public ResponseEntity recommend(@RequestParam(defaultValue = "1") Integer page,
                                    @RequestParam(defaultValue = "10") Integer pagesize) {
        PageResult pr = publishService.findRecommendMovements(page, pagesize);
        return ResponseEntity.ok(pr);
    }

    /**
     * 对圈子点赞
     *
     * @param publishId
     * @return
     */
    @GetMapping("/{id}/like")
    public ResponseEntity<Long> likeComment(@PathVariable("id") String publishId) {
        Long likeCount = commentsService.likeComment(publishId);
        return ResponseEntity.ok(likeCount);
    }

    /**
     * 取消点赞
     *
     * @param publishId
     * @return
     */
    @GetMapping("/{id}/dislike")
    public ResponseEntity<Long> disLikeComment(@PathVariable("id") String publishId) {
        Long likeCount = commentsService.disLikeComment(publishId);
        return ResponseEntity.ok(likeCount);
    }


    /**
     * 喜欢
     *
     * @param publishId
     * @return
     */
    @GetMapping("/{id}/love")
    public ResponseEntity<Long> loveComment(@PathVariable("id") String publishId) {
        Long likeCount = commentsService.loveComment(publishId);
        return ResponseEntity.ok(likeCount);
    }

    /**
     * 取消喜欢
     *
     * @param publishId
     * @return
     */
    @GetMapping("/{id}/unlove")
    public ResponseEntity<Long> disLoveComment(@PathVariable("id") String publishId) {
        Long likeCount = commentsService.unLoveComment(publishId);
        return ResponseEntity.ok(likeCount);
    }

    /**
     * 查询单条动态信息
     *
     * @param publishId
     * @return
     */
    @GetMapping("/{id}")
    public ResponseEntity<QuanZiVo> queryById(@PathVariable("id") String publishId) {
        QuanZiVo movements = publishService.queryById(publishId);
        return ResponseEntity.ok(movements);
    }

    /**
     * 谁看过我
     * movements/visitors
     * @return
     */
    @GetMapping("visitors")
    public ResponseEntity visitors() {
        List<VisitorsVo> visitorsVos =  publishService.visitorsTop();
        return ResponseEntity.ok(visitorsVos);
    }


    /**
     * 查看自己的圈子信息
     *
     * @param page
     * @param pageSize
     * @param userId
     * @return
     */
    @GetMapping("/all")
    public ResponseEntity queryAlbumList(@RequestParam(value = "page", defaultValue = "1") Integer page,
                                         @RequestParam(value = "pagesize", defaultValue = "10") Integer pageSize,
                                         @RequestParam(value = "userId") Long userId) {
        PageResult pageResult = publishService.queryAlbumList(userId, page, pageSize);
        return ResponseEntity.ok(pageResult);
    }
}
